import os,time
def func():
    print('process {} starts'.format(os.getpid()))
    time.sleep(2)
    print('process {} ends'.format(os.getpid()))


if __name__ == '__main__':
    from multiprocessing import Process, cpu_count, Pool
    print('main process is {}'.format(os.getpid()))
    print('core number is {}'.format(cpu_count()))  # 电脑有16个cpu（任务管理器中可以看到16个逻辑处理单元）
    start_time = time.time()
    ### multiprocess pool
    p = Pool(16)
    for i in range(24):#24个任务，进程池为16，因此只需要两轮，所以运行时间比4秒略大。
        p.apply_async(func)
    p.close()
    p.join()
    end_time = time.time()
    print('total time is {}'.format(str(end_time - start_time)))
